package com.lagou.work02
/*
第二题、人机猜拳
1.1 作业需求
 1. 选取对战角色
 2. 开始对战，用户出拳，与对手进行比较，提示胜负信息
 3. 猜拳结束算分，平局都加一分，获胜加二分，失败不加分
 4 . 循环对战，当输入“n”时，终止对战，并显示对战结果
 5. 游戏结束后显示得分
 */
import scala.io.StdIn
import scala.util.Random

/*
user类
 */
case class User(var name:String,var score:Int) {

  //定义数组记录玩家出拳
  var result = new Array[Int](3)

  //定义showFist方法，对出拳作模式匹配
  def showFist:Int ={
    var fist = StdIn.readInt()
    fist match {
      case 1 => println(s"$name 出拳：剪刀")
      case 2 => println(s"$name 出拳：石头")
      case 3 => println(s"$name 出拳：布")
      case _ => fist = 3
        println("输入不合法，默认出布")
    }
    fist
  }

}

case class Computer(var name:String,var score:Int){
  //定义数组记录机器出拳
  var result = new Array[Int](3)
  //定义showFist方法，对出拳作模式匹配
  def showFist:Int ={
    println(s"$name 出拳！")
    val fist = Random.nextInt(3) + 1
    fist match {
      case 1 => println(s"$name 出拳：剪刀")
      case 2 => println(s"$name 出拳：石头")
      case 3 => println(s"$name 出拳：布")
    }
    fist
  }

}

case class Game(gamer1: User,gamer2:Computer,var times:Int){

  //定义initgame方法，初始化游戏
  def initGame:Unit ={
    println("--------------欢迎进入游戏世界--------------")
    println("*******************************************")
    println("******************猜拳开始******************")
    println("*******************************************")

    var flag = true
    //对战双方初始成绩为0
    gamer1.score = 0
    gamer2.score = 0

    while (flag){
      println("请选择对战角色：（1.刘备  2.关羽  3.张飞）")
      //定义角色，根据输入判断选择对战
      val role = StdIn.readInt()
      role match {
        case 1 => gamer2.name = "刘备"
        case 2 => gamer2.name = "关羽"
        case 3 => gamer2.name = "张飞"
        case _ => println("不存在此角色，默认刘备")
          gamer2.name = "刘备"
      }
      //if判断是否开始游戏
      println("要开始么？y/n")
      val confirm = StdIn.readChar()
      if (confirm == 'y'){
        beginGame
        flag = false
      }else{
        println("要退出游戏么？y/n")
        if(StdIn.readChar() == 'y')
          flag = false
      }
    }
    endGame
  }

  //定义begingame方法，判断游戏输赢
  def beginGame:Unit ={
    var flag = true
    while (flag){
      println("请出拳!  1.剪刀  2.石头  3.布")
      val fist1 = gamer1.showFist
      val fist2 = gamer2.showFist

      //result记录对战双方出拳得分，匹配条件判断输赢
      val result = judge(fist1,fist2)
      result match {
        case _ if(result < 0) => gamer1.result(2) += 1
          gamer2.result(0) += 1
          println("结果：你输了!")
        case _ if(result > 0) =>  gamer1.result(0) += 1
          gamer2.result(2) += 1
          println("结果：恭喜你赢了!")
        case _ => gamer1.result(1) += 1
          gamer2.result(1) += 1
          println("结果：平局!")
      }
      times += 1
      println("是否开始下一轮？y/n")
      if(StdIn.readChar() == 'n')
        flag = false
    }
  }

  //定义endgame方法结束游戏
  def endGame:Unit ={
    println("退出游戏!")
    println("------------------------------------------")
    println(s"${gamer2.name} VS ${gamer1.name}")
    println(s"对战次数${times}次")

    println("姓名  分数 胜局 和局 负局")
    //计算对战双方得分
    gamer1.score = gamer1.result(0) *2 + gamer1.result(1)
    println(s"${gamer1.name}\t${gamer1.score}\t${gamer1.result(0)}\t${gamer1.result(1)}\t${gamer1.result(2)}")

    gamer2.score = gamer2.result(0) *2+ gamer2.result(1)
    println(s"${gamer2.name}\t${gamer2.score}\t${gamer2.result(0)}\t${gamer2.result(1)}\t${gamer2.result(2)}")
  }

  //定义judge方法计分
  def judge(fist1: Int, fist2: Int):Int ={
    var result = fist1 - fist2
    if(Math.abs(result) >= 2)
      result = (fist1 % 3) - (fist2 % 3)
    result
  }
}

object Mora {

  def main(args: Array[String]): Unit = {

    val user = User("游客",0)
    val computer = Computer(null,0)
    val game = Game(user,computer,0)

    game.initGame

  }
}
